package com.asap.demo.mysql.util;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: TimeUtil   
 * @Description: TODO  
 * @author: robin  
 * @date:2018年3月13日 下午9:58:41
 */
public final class TimeUtil {
	private static final Logger logger = LoggerFactory.getLogger(TimeUtil.class);

	private TimeUtil() {}
	
	private static ThreadLocal<Map<String, SimpleDateFormat>> sdfMap = new ThreadLocal<Map<String, SimpleDateFormat>>() {

		@Override
		protected Map<String, SimpleDateFormat> initialValue() {
			return new HashMap<String, SimpleDateFormat>();
		}

	};

 

	/**

	 * 返回一个SimpleDateFormat,每个线程只会new一次pattern对应的sdf

	 * 

	 * @param pattern

	 * @return

	 */

	private static SimpleDateFormat getSdf(final String pattern) {

		Map<String, SimpleDateFormat> tl = sdfMap.get();
		SimpleDateFormat sdf = tl.get(pattern);
		if (sdf == null) {
			sdf = new SimpleDateFormat(pattern);
			tl.put(pattern, sdf);

		}

		return sdf;

	}

 
	private static final String GMT_DATEFORMAT = "MMM dd yyyy HH:mm:ss z";
	private static final String COMMON_DATEFORMAT = "yyyy-MM-dd HH:mm:ss"; 
	
	/**
	 * @Title: convertString2Date   
	 * @Description: TODO  
	 * @param timestamp 时间抽字符串
	 * @return Date  
	 * @throws
	 */
	public static Date convertString2Date(String timestamp) {
		try {
			return convertString2Date(timestamp, timestamp.indexOf("GMT") > 0 ? getSdf(GMT_DATEFORMAT) : getSdf(COMMON_DATEFORMAT));
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static String getCurrentTime(){
		SimpleDateFormat sdf = new SimpleDateFormat(COMMON_DATEFORMAT);
		return sdf.format(new Date());
	}
	
	/**
	 * @Title: countTimeWindow   
	 * @Description: TODO  
	 * @param timestamp timestamp
	 * @param timespan timestamp
	 * @param unit unit
	 * @return long  
	 * @throws
	 */
	public static long countTimeWindow(String timestamp, int timespan, String unit) {
		
		return countTimeWindow(timestamp, 
				toMillisecond(timespan, unit));
	}
	
	/**
	 * @Title: convertString2Date   
	 * @param timestamp timestamp
	 * @param format format
	 * @return Date  Date
	 * @throws ParseException ParseException
	 */
	private static Date convertString2Date(String timestamp, DateFormat format) throws ParseException {
		logger.debug("convertString2Date:"+timestamp+";format:"+format.toString());
		return format.parse(timestamp);
	}
	
	/**
	 * @Title: countTimeWindow   
	 * @Description: TODO  
	 * @param  timestamp timestamp
	 * @param  timespan timespan
	 * @return long  
	 * @throws
	 */
	public static long countTimeWindow(String timestamp, long timespan) {
		logger.debug("countTimeWindow()"+timestamp);
		Date d=convertString2Date(timestamp);
		if(d==null)
			return 0;
		else
			return d.getTime() / timespan;
	}
	
	/**
	 * @Title: toMillisecond   
	 * @Description: TODO  
	 * @param  timespan timespan
	 * @param  unit unit
	 * @return long  long
	 * @throws
	 */
	public static long toMillisecond(int timespan, String unit) {
		
		return map.get(unit)*timespan;
	}
	
	private static HashMap<String, Long> map;
	
	private static final long S = 1000L;
	private static final long M = 60000L;
	private static final long H = 3600000L;
	private static final long D = 86400000L;
	private static final long W = 604800000L;
	
	static {
		map = new HashMap<>();
		map.put("s", S);
		map.put("m", M);
		map.put("h", H);
		map.put("d", D);
		map.put("w", W);
	}
	
	
}
